<?php

class Help extends CI_Controller {
	
	 public function __construct(){
		parent::__construct();		
		if(!is_admin_role('admin'))
			redirect(base_url('admin/login'));
		$this->load->model('Help_m', 'help');
		$this->load->helper('util');
    }
	
	function index() {
		$data = array();
		$data['helps']		= $this->help->get_helps();
		$data['main_content'] = 'admin/help';
      $this->load->view('admin/includes/template', $data);
	}
	
	function form($id = false){
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		//set the default values
		$data['id']				=	'';
		$data['module']		=	'';
		$data['title']			=	'';
		$data['slug']			=	'';
		$data['content']		=	'';
		$data['seo_title']	=	'';
		$data['meta']			=	'';
		$data['new_window']	=	'';
		$data['sequence']		=	'';
		$data['trang_thai']	=  '';
		$data['page_title']	= 'Help Form';
		$data['help']			= $this->help->get_help($id);
		
		if($id){
			$help	= $this->help->get_help($id);
			if(!$help){
				//Help does not exist
				$this->session->set_flashdata('error', 'Help không tìm thấy');
				redirect('admin/help');
			}			
			//set values to db values
			$data['id']				= 	$help->id;
			$data['module']		= 	$help->module;
			$data['title']			= 	$help->title;
			$data['slug']			= 	$help->slug;
			$data['content']		= 	$help->content;
			$data['seo_title']	= 	$help->seo_title;
			$data['meta']			= 	$help->meta;
			$data['sequence']		=	$help->sequence;
			$data['trang_thai']	=	$help->trang_thai;
		}
		$this->form_validation->set_rules('module', 'Module', 'trim|required');
		$this->form_validation->set_rules('title', 'Tiêu đề', 'trim|required');		
		$this->form_validation->set_rules('slug', 'Slug', 'trim');
		$this->form_validation->set_rules('content', 'Nội dung', 'trim|required');
		$this->form_validation->set_rules('seo_title', 'Seo title', 'trim');
		$this->form_validation->set_rules('meta', 'Meta', 'trim');
		$this->form_validation->set_rules('sequence', 'Sequence', 'trim|integer');
		
		
		// Validate the form
		if($this->form_validation->run() == false){
			$data['main_content'] = 'admin/help_form';
			$this->load->view('admin/includes/template', $data);
		}else{
			$this->load->helper('text');			
			//first check the slug field
			$slug = $this->input->post('slug');			
			//if it's empty assign the name field
			if(empty($slug) || $slug=='')
				$slug = $this->input->post('title');	
			$slug	= remove_accents($slug);
			$save = array();
			$save['id']				=	$id;
			$save['module']		=	$this->input->post('module');
			$save['title']			=	$this->input->post('title');
			$save['slug']			=	$slug;
			$save['content']		=	$this->input->post('content');
			$save['seo_title']	=	$this->input->post('seo_title');
			$save['meta']			=	$this->input->post('meta');
			$save['sequence']		=	$this->input->post('sequence');
			$save['trang_thai']	=	$this->input->post('trang_thai');
			//save the page
			$help_id	= $this->help->save($save);			
			$this->session->set_flashdata('message', 'Lưu dữ liệu thành công');			
			//go back to the page list
			redirect('admin/help');
		}
	}
	/********************************************************************
	delete page
	********************************************************************/
	function delete($id){
		$help	= $this->help->get_help($id);
		if($help){
			$this->help->delete_page($id);
			$this->session->set_flashdata('message', 'Xóa thành công');
		}else
			$this->session->set_flashdata('error', 'Có lỗi xảy ra');
		redirect('admin/help');
	}
}